<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/26 0026
 * Time: 上午 11:17
 */
namespace app\index\controller;

use app\admin\model\CategoryModule;
use app\admin\model\Lesson;
use app\admin\model\LessonCate;
use app\admin\model\News;

use app\admin\model\NewsCate;
use service\ToolsService;
use think\Db;
use app\admin\model\IndexBlock;

class Category extends Base
{
    protected $beforeActionList=['category'];

    public function index()
    {
        $pid      = $this->request->param( 'pid' , '' );
        $id       = $this->request->param( 'id' , '' );

        $catelist = CategoryModule::where( [ 'pid' => $pid , 'status' => 1 , 'is_deleted' => 1 ] )->order( 'sort asc,id asc' )->select()->toArray();
        $pcate    = CategoryModule::get( $pid )->toArray();

        $category = $pcate;

        $parent_id = $pid;

        $this->assign( [
            'pcate'     => $pcate ,
            'curid'     => intval( $id ) ,
            'cur'       => $category ,
            'catelist'  => $catelist ,
            'parent_id' => $parent_id ,
        ] );

        $this->index_filter( $category );
        return view( $category[ 'index_template' ] ?: '' );
    }

    /**
     * 分发请求
     * @param array $category
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function index_filter($category=[])
    {
        if($category[ 'id' ] == 1 || $category[ 'pid' ] == 1){
            $id   = $this->request->param( 'id' , '' );
            //查询所有数据
            $aryData = array();
            $aryData = Db::name( 'safety' )->where( [  'is_deleted' => 1 , 'status' => 1 ] )->order( 'sort asc,id asc' )->select();
            //var_dump($id);exit();
            $aryNow = $aryData[0];
            if($id>0){
                foreach ($aryData as $v){
                    if($v['id'] == $id){
                        $aryNow = $v;
                    }
                }
            }
            $this->assign( [
                'aryData' => $aryData ,
                'aryNow' => $aryNow ,
                'tid' =>1,
                'webTitle'=>"关于我们-湖南绿苑节能科技有限公司",
                'webKeywords'=>"关于我们，湖南绿苑节能科技有限公司",
                'webDecs'=>"关于我们，湖南绿苑节能科技有限公司",
            ] );
        }elseif ( $category[ 'pid' ] == 3 ||  $category[ 'id' ] == 3) {
            $id   = $this->request->param( 'id' , '' );
            //查询所有数据
            $aryData = array();
            $aryData = Db::name( 'lesson' )->where( [  'is_deleted' => 1 , 'status' => 1 ] )->order( 'sort asc,id asc' )->select();
            //获取当前类别的所有内容

            $aryNow = $aryData[0];
            if($id>0){
                foreach ($aryData as $v){
                    if($v['id'] == $id){
                        $aryNow = $v;
                    }
                }
            }

            $aryNowAll = array();
            $aryNowAll = Db::name('lesson_content_list')->field( 'content' )->where(['pid'=>$aryNow['id'],'is_deleted'=>1])->order('sort asc,id asc')->select();
            $aryNew = array();
            if(!empty($aryNowAll)){
                foreach ($aryNowAll as $k=>&$v){
                    $aryTest = array();
                    $aryTest = json_decode($v['content'],true);
                    $aryNew[] = $aryTest[0];
                }
            }

            $this->assign( [
                'aryData' => $aryData ,
                'aryNow' => $aryNow ,
                'aryNowAll' => $aryNew ,
                'tid' =>3,
                'webTitle'=>"业务范围-湖南绿苑节能科技有限公司",
                'webKeywords'=>"业务范围，湖南绿苑节能科技有限公司",
                'webDecs'=>"业务范围，湖南绿苑节能科技有限公司",
            ] );
        }
        elseif ( $category[ 'pid' ] == 2 ||  $category[ 'id' ] == 2) {
            $id   = $this->request->param( 'id' , 6 );
            $keywords   = $this->request->param( 'keywords' , '' );
            $pcate = array();
            if($id>0){
                $pcate    = CategoryModule::get( $id )->toArray();
            }

            $page = $this->request->get( 'page' , 1 );


            //获取当前选择的所有新闻并分页
            $aryNewsAll = array();

            if(!empty($keywords)){
                //查找相应的新闻分类
                //$strSqlData = "SELECT * FROM `news` WHERE `title` LIKE '%{$keywords}%' AND is_deleted = 1 AND status = 1 ORDER BY create_at desc LIMIT 0,6";
                $aryNewsAll = News::where( [ 'title' => " LIKE '%{$keywords}%'" , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select()->toArray();
            }else{
               if(!empty($pcate)){
                   $aryNewsId = NewsCate::where( [ 'title' => $pcate[ 'name' ] , 'is_deleted' => 1 , 'status' => 1 ] )->page( 0 , 1 )->find();

                   if ( !empty( $aryNewsId ) ) {
                       //查找相应的新闻分类
                       $aryNewsAll = News::where( [ 'pid' => $aryNewsId[ 'id' ] , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select()->toArray();
                   }
               }
            }

            //获取当前分类信息
            $aryCatNews = array();
            $aryCatNews = CategoryModule::getChildren(2);
            $intpage = $page+1;
            $strPages = "/ajaxnews?"."id={$id}&keywords={$keywords}&page={$intpage}";

                $this->assign( [
                'aryNewsAll' => $aryNewsAll ,
                'keywords' => $keywords,
                'aryCatNews'   => $aryCatNews,
                    'strPages'   => $strPages,
                    'webTitle'=>"新闻资讯-湖南绿苑节能科技有限公司",
                    'webKeywords'=>"新闻资讯，湖南绿苑节能科技有限公司",
                    'webDecs'=>"新闻资讯，湖南绿苑节能科技有限公司",
            ] );
            $cate = '';
        }elseif ( $category[ 'pid' ] == 4 ||  $category[ 'id' ] == 4) {
            $page = $this->request->get( 'page' , 1 );

            //获取当前选择的所有新闻并分页
            $aryCasesAll = array();

            //查找相应的新闻分类
            $aryCasesAll = Db::name( 'cases' )->where( [  'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select();
            $block4 = IndexBlock::index('6');
            $intpage = $page+1;
            $strPages = "/ajaxcases?"."page={$intpage}";
            $block8 = IndexBlock::index('2');
            $this->assign( [
                'aryCasesAll' => $aryCasesAll ,
                'strPages'   => $strPages,
                'block8'=>$block8,
                'block4'=>$block4,
                'webTitle'=>"成功案例-湖南绿苑节能科技有限公司",
                'webKeywords'=>"成功案例，湖南绿苑节能科技有限公司",
                'webDecs'=>"成功案例，湖南绿苑节能科技有限公司",
            ] );
            $cate = '';
        }elseif ( $category[ 'pid' ] == 5 ||  $category[ 'id' ] == 5) {
            $this->assign( [

                'webTitle'=>"联系我们-湖南绿苑节能科技有限公司",
                'webKeywords'=>"联系我们，湖南绿苑节能科技有限公司",
                'webDecs'=>"联系我们，湖南绿苑节能科技有限公司",
            ] );
        }
    }

    public function ajaxcases(){
        $page = $this->request->get( 'page' , 1 );
        $msg = ['code' => 0, 'msg' => "", 'url' => ""];
        if ($this->app['request']->isAjax()) {
            $aryCasesAll = Db::name( 'cases' )->where( [  'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select();
            $strPages = "";
            $strView = "";
            if(!empty($aryCasesAll)){
                $intpage = $page+1;
                $strPages = "/ajaxcases?"."page={$intpage}";


                $this->assign( [
                    'aryCasesAll' => $aryCasesAll ,
                ] );
                $strView = $this->fetch("ajaxcases" );
            }
            $msg = ['code' => 1, 'msg' => $strView, 'url' => $strPages];
            return json($msg);
        }
        return json($msg);
    }

    public function ajaxNews(){
        $id   = $this->request->param( 'id' , '' );
        $keywords   = $this->request->param( 'keywords' , '' );
        $page = $this->request->get( 'page' , 1 );
        $msg = ['code' => 0, 'msg' => "", 'url' => ""];
        if ($this->app['request']->isAjax()) {

            //获取当前选择的所有新闻并分页
            $aryNewsAll = array();

            if(!empty($keywords)){
                //查找相应的新闻分类
                //$strSqlData = "SELECT * FROM `news` WHERE `title` LIKE '%{$keywords}%' AND is_deleted = 1 AND status = 1 ORDER BY create_at desc LIMIT 0,6";
                $aryNewsAll = News::where( [ 'title' => " LIKE '%{$keywords}%'" , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select()->toArray();
            }else{
                $pcate = array();
                if($id>0){
                    $pcate    = CategoryModule::get( $id )->toArray();
                }
                if(!empty($pcate)){
                    //获取列表名称
                    $aryNewsId = NewsCate::where( [ 'title' => $pcate[ 'name' ] , 'is_deleted' => 1 , 'status' => 1 ] )->page( 0 , 1 )->find();

                    if ( !empty( $aryNewsId ) ) {
                        //查找相应的新闻分类
                        $aryNewsAll = News::where( [ 'pid' => $aryNewsId[ 'id' ] , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select()->toArray();
                    }
                }
            }

            $strPages = "";
            $strView = "";
            if(!empty($aryNewsAll)){
                $intpage = $page+1;
                $strPages = "/ajaxnews?"."id={$id}&keywords={$keywords}&page={$intpage}";

                $this->assign( [
                    'aryNewsAll' => $aryNewsAll ,
                ] );
                $strView = $this->fetch("ajaxnews" );
            }
           // $this->ajaxReturn($data,'json');
            $msg = ['code' => 1, 'msg' => $strView, 'url' => $strPages];
            return json($msg);
        }
        return json($msg);
    }

    public function ajaxcate($pid=0){
        empty($pid) && $pid = $this->request->post('pid');
        $lesson_cate =LessonCate::where(['is_deleted'=>1,'status'=>1])->field('id,name,pid')->order('sort asc,id asc')->select()->toArray();
        $lesson_cate = ToolsService::arr2tree($lesson_cate,'id','pid','children');
        $cate = [];
        foreach ($lesson_cate as $k=>$v){
            if ($v['id']==$pid && isset($v['children'])){
                $cate = $v['children'];
            }
        }
        return $cate;
    }

    /**
     * 课程详情控制器
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lesson()
    {
        $id       = $this->request->param( 'id' , '' );
        $db        = Lesson::where( [ 'id' => $id , 'is_deleted' => 1 , 'status' => 1 ] )->find()->visible( [ 'id' , 'title' , 'desc' , 'leidatu' , 'slider' , 'type' , 'keywords' , 'pid' ] )->toArray();
        $recommend = Lesson::where( [ 'is_deleted' => 1 , 'status' => 1 , 'is_recommend' => 1 ] )->field( 'id,title,cover,cover_desc' )->order( 'sort asc,id asc' )->select()->toArray();
        if ( $db[ 'type' ] == 12 ) {
            $pcate = CategoryModule::get( 12 );
            $this->assign( [
                'pcate' => $pcate ,
                'cur'   => [ 'name' => '课程详情' , 'id' => '' ] ,
            ] );
        } elseif ( $db[ 'type' ] == 39 ) {
            $pcate = CategoryModule::get( 39 );
            $this->assign( [
                'pcate' => CategoryModule::get( $pcate[ 'pid' ] ) ,
                'cur'   => $pcate ,
            ] );
        }
        $db[ 'slider' ]  = explode( '|' , $db[ 'slider' ] );
        $db[ 'desc' ]    = json_decode( $db[ 'desc' ] , true );
        $leidatu_value   = array_column( json_decode( $db[ 'leidatu' ] , true ) , 'value' );
        $db[ 'leidatu' ] = json_decode( $db[ 'leidatu' ] , true );
        foreach ( $db[ 'leidatu' ] as $k => $v ) {
            unset( $db[ 'leidatu' ][ $k ][ 'value' ] );
            $db[ 'leidatu' ][ $k ][ 'max' ] = 10;
        }
        $lesson_content = Db::name( 'lesson_content_list' )->where( [ 'pid' => $id , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'sort asc,id asc' )->select();
        foreach ( $lesson_content as $k => $v ) {
            $lesson_content[ $k ][ 'content' ] = json_decode( $v[ 'content' ] , true );
        }
        foreach ( $recommend as $k => $v ){
            $recommend[$k]['cover_desc'] = json_decode( $v['cover_desc'] , true );
        }
        $db['time_name'] = Lesson::getLessonAttr( $db['pid'] );
        $db['classList'] = Lesson::getLessonAttr( $db['pid'] , 'nianji' );
        $this->assign( [
            'db'             => $db ,
            'leidatu_value'  => $leidatu_value ,
            'lesson_content' => $lesson_content ,
            'recommend'      => $recommend ,
        ] );
        return view( "lesson/lessonDetail" );
    }

    public function about(){
//        $secondMenu = Db::name('category_module')->field('name,id')->where('id', 9)->find();
//        $pcate = Db::name('category_module')->where('id',1)->find();
//        $school = Db::name('cate_list')->where(['pid' => 35, 'is_deleted' => 1, 'status' => 1])->order('sort asc,id asc')->select();
//        foreach ($school as $k=>$v){
//            $school[$k]['content'] = json_decode($v['content'], true);
//        }
//        $this->assign([
//            'pcate'=>$pcate,
//            'cur'=>['name'=>'合作学校','id'=>35],
//            'secondMenu'=>$secondMenu,
//            'school'=>$school
//        ]);
        return view();
    }
}